library(plyr)
library(estimatr)
library(emmeans)
library(tidyverse)
library(magrittr)
library(broom)

d1 <- "https://github.com/thomasjwood/full_fact/raw/main/ff_mt.rds" %>% 
  url %>% 
  gzcon %>% 
  readRDS %>% 
  mutate(
    out_agree = out_agree %>% 
      mapvalues(
        c("Tend to disagree",
          "Tend to agree"),
        c("Disagree",
          "Agree")
      ) %>% 
      factor(
        c("Strongly disagree",
          "Disagree",
          "Neither agree nor disagree",
          "Agree",
          "Strongly agree")
      ),
    out_true = out_true %>% 
      factor(
        c("True",
          "Probably true",
          "Not sure",
          "Probably false",
          "False") %>% 
          rev
      ),
    agree_num = out_agree %>% 
      as.numeric,
    true_num = out_true %>% 
      as.numeric,
    cond = cond %>% 
      factor(
        c("cond_items",
          "cond_misinfo",
          "cond_corr")
      )
  )

cor(d1$agree_num, d1$true_num)

d1$com_num <- d1$agree_num %>% 
  add(
    d1$true_num
  ) %>% 
  divide_by(2)


# attrition check--keep only respondents who did both waves ---------------

d1 %<>% 
  left_join(
    d1 %>% 
      select(
        country, caseid, wave
      ) %>% 
      group_by(
        country, caseid, wave
      ) %>% 
      slice(1) %>% 
      group_by(
        country, caseid
      ) %>% 
      summarize(
        waves_done = wave %>% unique %>% length
        ) %>% 
      filter(
        waves_done == 2
      )
    ) %>% 
  filter(
    waves_done %>% 
      is.na %>% 
      not
  ) %>% 
  select(-waves_done)
  

# setting up modelling table ----------------------------------------------


t1 <- d1 %>% 
  mutate(
    country = issue %>%
      str_detect(
        "global|saltwater"
      ) %>%
      ifelse(
        "Multi-country",
        country
      )
  ) %>%
  group_by(
    country, wave, issue
  ) %>% 
  nest 

t1$mods <- t1$data %>% 
  map(
    ~lm_robust(
      com_num ~ cond, 
      data = .x
    )
  )

t1$emm <- t1$mods %>% 
  map2(
    t1$data,
    ~emmeans(
      .x, 
      consec ~ cond, 
      data = .y
    )
  )

t1$edf <- t1$emm %>% 
  map(
    ~extract2(., 1) %>% 
      tidy
  )

t1$cont <- t1$emm %>% 
  map(
    ~extract2(., 2) %>% 
      tidy
  )


# setting up data for plotting --------------------------------------------


t2 <- t1 %>% 
  ungroup %>% 
  select(
    country, issue, cont, wave
  ) %>% 
  pmap_dfr(
    \(country, issue, cont, wave)
      
      cont %>% 
      mutate(
        issue = issue, 
        country = country,
        wave = wave
      )
  ) %>% 
  rename(p.value = "adj.p.value") %>% 
  filter(
    contrast %>% 
      equals(
        "cond_corr - cond_misinfo"
      )
  )

t2$country %<>% 
  fct_reorder(
    t2$estimate, .desc = F
  )


t2$pgroup <- t2$p.value %>% 
  is_less_than(.05) %>% 
  ifelse(
    "significant",
    "insignificant"
  )

t2$wn <- t2$wave %>% 
  factor %>% 
  as.numeric %>% 
  add(
    t2$pgroup %>% 
      equals("significant") %>% 
      ifelse(
        .1, 0
      )
  )

t2_2 <- t2 %>% 
  select(
    estimate, issue:wave
  ) %>% 
  spread(wave, estimate) %>% 
  rename_at(
    str_c("wave ", 1:2),
    ~str_c("wave ", 1:2) %>% 
      str_replace_all(
        " ", "_"
      )
  ) %>% 
  na.omit %>% 
  mutate(
    ic = str_c(
      issue, "_", country
    )
  )

t2 %<>% 
  mutate(
    ic = str_c(
      issue, "_", country
    )
  ) %>% 
  filter(
    ic %>% 
      is_in(
        t2_2$ic
      )
  ) %>% 
  select(-ic)

t2_2 %<>% 
  gather(
    wave, est, starts_with("wave")
  ) %>% 
  mutate(
    wave = wave %>% 
      str_replace("_", " ")
  ) %>% 
  left_join(
    t2 %>% 
      select(
        wave, issue, country, wn
      )
  )

t2l <- t2 %>% 
  group_by(
    wave, country
  ) %>% 
  summarize(
    mu = estimate %>% mean
  ) %>% 
  mutate(
    lab = mu %>% 
      round(2) %>% 
      str_replace("0\\.", ".") %>% 
      str_pad(width = 4, side = "right", pad = "0")
  )

t2 %>% 
  ggplot() +
  geom_hline(
    yintercept = 0,
    linetype = "dotted"
  ) +
  geom_blank(
    aes(wave, estimate)
  ) +
  geom_line(
    aes(
      wn, est, group = issue
    ),
    alpha = .5,
    data = t2_2
  ) +
  geom_point(
    aes(wn, estimate, fill = pgroup),
    shape = 21,
    size = 3,
  ) +
  geom_text(
    aes(
      y = mu, label = lab
    ),
    shape = 21,
    fill = "white",
    size = 3,
    x = .75,
    data = t2l %>% 
      filter(
        wave == "wave 1"
      ),
    fontface = "bold"
  ) +
  geom_text(
    aes(
      y = mu, label = lab
    ),
    shape = 21,
    fill = "white",
    size = 3,
    x = 2.45,
    data = t2l %>% 
      filter(
        wave != "wave 1"
      ),
    fontface = "bold"
  ) +
  scale_fill_manual(
    values = c("grey99", "grey40") %>% 
      rev,
    breaks = t2$pgroup %>% 
      unique,
    labels = c("Significant: p < .05",
               "Insignificant: p >= .05"
    )
  ) +
  scale_x_discrete(
    breaks = t2$wave %>% 
      factor %>% 
      levels,
    labels = str_c(
      "Wave ", 1:2
    )
  ) +
  facet_wrap(~country, nrow = 1) +
  labs(
    x = "",
    y = "Correction effect",
    fill =""
  ) +
  theme(
    strip.text.y = element_text(
      size =  11, 
      angle = 0
      ),
    legend.position = "bottom",
    legend.margin = margin(t = -.3, unit = "cm")
  )

